﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArcannEngine.Entities
{
    public class Engine
    {
        public enum EngineType
        {
            STARTER,
            REGULAR
        }

        float fuelBurnin = 500f; //[kg/s]
        //RocketEntity rocket;
        EngineType type;
        float v_fuel = 5000; //[m/s] - Isp
        public float mFuel = 0;
        float P = 0;
        bool running = false;
        private Element element;

       /* public Engine(RocketEntity rocket, float fuelBurnin, float mFuel)
        {
            this.rocket = rocket;
            this.type = EngineType.STARTER;
            this.fuelBurnin = fuelBurnin;
            this.mFuel = mFuel;
            this.running = true;
        }

        public Engine(RocketEntity rocket, float mFuel, float power, bool b)
        {
            this.rocket = rocket;
            this.type = EngineType.REGULAR;
            this.mFuel = mFuel;
            this.P = power;
        }*/

        public Engine(Element element, EngineType type, float value)
        {
            this.element = element;
            this.type = type;
            if (type == EngineType.REGULAR)
            {
                this.P = value;
            }
            else
            {
                this.fuelBurnin = value/2;
                this.P = value;
            }
        }

        public float run(int time)
        {
            float result = 0;
            if (element.fuelContainer > 0)
            {
                running = true;
                switch (type)
                {
                    case EngineType.STARTER:
                        {
                            break;
                        }
                    case EngineType.REGULAR:
                        {
                            float tempFuelBurnin = fuelBurnin;
                            //fuelBurnin = ((element.rocket.f_g + element.rocket.f_t) / v_fuel);
                            fuelBurnin *= 1.1f;
                            /*while (v_fuel * fuelBurnin > P)
                            {
                                fuelBurnin *= 0.9f;
                            }*/
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }

                result = burn();
                //result = P * 1000f;
            }
            else
            {
                running = false;
                element.disconnected = true;
            }
            return result;
        }

        private float burn()
        {
            if (element.fuelContainer > fuelBurnin)
            {
                element.fuelContainer -= fuelBurnin;
                return fuelBurnin * v_fuel;
            }
            else
            {
                float temp = element.fuelContainer;
                element.fuelContainer = 0;
                element.disconnected = true;
                return temp * v_fuel;
            }
        }

        public bool isRunning()
        {
            return running;
        }
    }
}
